<template>
  <div class="user-address">
    <div class="header-box">
      <h1 class="title">收货地址</h1>
      <el-button
        class="header-box-btn"
        type="primary"
        plain
        @click="dialogAddNewAddressFormVisible = true"
        v-if="$store.state.userShippingAddress.length > 0"
      >
        添加新地址
      </el-button>
    </div>

    <div class="address-box" v-if="$store.state.userShippingAddress.length > 0">
      <el-card
        class="address-card"
        shadow="hover"
        v-for="item in $store.state.userShippingAddress"
        :key="item.addressId"
      >
        <div slot="header" class="address-clearfix">
          <el-tag type="success">{{ item.tag }}</el-tag>
          <el-button
            class="address-delet-btn el-icon-delete"
            style="float: right; padding: 5px 5px"
            type="danger"
            circle
            @click="deleteAddress(item.addressId)"
          >
          </el-button
          ><el-button
            class="address-edit-btn el-icon-edit"
            style="float: right; padding: 5px 5px"
            type="primary"
            circle
            @click="getEditAddressInfo(item.addressId)"
          ></el-button>
        </div>
        <div class="address-detail">
          <el-descriptions class="margin-top" :column="2" border>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-user"></i>
                姓名
              </template>
              {{ item.name }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-location-outline"></i>
                收货地址
              </template>
              {{ item.address }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-mobile-phone"></i>
                手机号
              </template>
              {{ item.phone }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-office-building"></i>
                详细地址
              </template>
              {{ item.detailAddress }}
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </el-card>
    </div>

    <el-empty
      description="收货地址为空"
      v-if="$store.state.userShippingAddress.length == 0"
    >
      <el-button
        type="primary"
        plain
        @click="dialogAddNewAddressFormVisible = true"
      >
        添加新地址
      </el-button>
    </el-empty>

    <!-- 添加新地址弹窗 -->
    <el-dialog
      title="添加收货地址"
      center
      width="30%"
      :visible.sync="dialogAddNewAddressFormVisible"
      :before-close="handleClose"
      @closed="clearFormCache"
    >
      <el-form
        :model="newAddressForm"
        ref="newAddressForm"
        size="medium"
        label-position="left"
        label-width="80px"
        :rules="rules"
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="newAddressForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="newAddressForm.phone"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input
            v-model="newAddressForm.address"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input
            type="textarea"
            autosize
            v-model="newAddressForm.detailAddress"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="标签" prop="tag">
          <el-input
            v-model="newAddressForm.tag"
            autocomplete="off"
            placeholder="如“家”、“学校”。限五个字内"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddNewAddressFormVisible = false"
          >取 消</el-button
        >
        <el-button
          type="primary"
          @click="submitNewAddressForm('newAddressForm')"
        >
          确 定
        </el-button>
      </div>
    </el-dialog>

    <!-- 修改地址弹窗 -->
    <el-dialog
      title="修改收货地址"
      center
      width="30%"
      :visible.sync="dialogEditAddressFormVisible"
      :before-close="handleClose"
      @closed="clearFormCache"
    >
      <el-form
        :model="editAddressForm"
        ref="editAddressForm"
        size="medium"
        label-position="left"
        label-width="80px"
        :rules="rules"
      >
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="editAddressForm.name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="editAddressForm.phone"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input
            v-model="editAddressForm.address"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input
            type="textarea"
            autosize
            v-model="editAddressForm.detailAddress"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="标签" prop="tag">
          <el-input
            v-model="editAddressForm.tag"
            autocomplete="off"
            placeholder="如“家”、“学校”。限五个字内"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditAddressFormVisible = false">
          取 消
        </el-button>
        <el-button
          type="primary"
          @click="submitEditAddressForm('editAddressForm')"
        >
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "UserAddress",
  data() {
    return {
      dialogAddNewAddressFormVisible: false,
      newAddressForm: {
        addressId: "",
        name: "",
        phone: "",
        address: "",
        detailAddress: "",
        tag: "",
      },
      dialogEditAddressFormVisible: false,
      editAddressForm: {
        addressId: "",
        name: "",
        phone: "",
        address: "",
        detailAddress: "",
        tag: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入名字", trigger: "blur" },
          { min: 1, max: 15, message: "长度在1到12个字内", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "请输入正确的手机号", trigger: "blur" },
        ],
        address: [{ required: true, message: "请输入地址", trigger: "blur" }],
        tag: [{ max: 5, message: "长度限制在5个字内", trigger: "blur" }],
      },
    };
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    submitNewAddressForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const TSSUSA = this.$store.state.userShippingAddress.length;
          console.log(TSSUSA == 0);
          this.newAddressForm.addressId =
            TSSUSA == 0
              ? 1
              : this.$store.state.userShippingAddress[0].addressId + 1;
          // 坑 如果不使用 Object.assign({}, this.newAddressForm) address将会被绑到与this.form一样的引用地址，修改this.form也等于修改address
          const address = Object.assign({}, this.newAddressForm);
          console.log(address);
          this.$store.commit("handleAddNewAddress", address);
          this.dialogAddNewAddressFormVisible = false;
          this.$message({
            message: "添加地址成功",
            type: "success",
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    clearFormCache() {
      setTimeout(() => {
        // 清空newAddressForm
        this.newAddressForm.addressId = "";
        this.newAddressForm.name = "";
        this.newAddressForm.phone = "";
        this.newAddressForm.address = "";
        this.newAddressForm.detailAddress = "";
        this.newAddressForm.tag = "";
      }, 500);
    },
    deleteAddress(addressId) {
      // 执行删除操作弹出确认框
      this.$confirm("确认删除所选地址吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 确认后进行的操作
          this.$store.commit("deleteAddress", addressId);
        })
        .catch(() => {
          // 取消后进行的操作
        });
    },
    getEditAddressInfo(addressId) {
      this.dialogEditAddressFormVisible = true;
      this.$store.commit("getEditAddressInfo", addressId);
      const TEAF = this.editAddressForm;
      const TSSUEAI = this.$store.state.userEditAddressInfo;
      TEAF.addressId = TSSUEAI.addressId;
      TEAF.name = TSSUEAI.name;
      TEAF.phone = TSSUEAI.phone;
      TEAF.address = TSSUEAI.address;
      TEAF.detailAddress = TSSUEAI.detailAddress;
      TEAF.tag = TSSUEAI.tag;
    },
    submitEditAddressForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const editedAddress = Object.assign({}, this.editAddressForm);
          // console.log(editedAddress);
          this.$store.commit("editAddress", editedAddress);
          this.dialogEditAddressFormVisible = false;
          this.$message({
            message: "修改地址成功",
            type: "success",
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.user-address {
  padding: 20px;
  position: relative;
}
.header-box {
  height: 68px;
}
.header-box .title {
  float: left;
  font-size: 30px;
  font-weight: 400;
  line-height: 68px;
  color: #757575;
}
.header-box-btn {
  float: right;
  margin-top: 11px;
}

.address-card {
  margin-bottom: 20px;
}

.address-edit-btn {
  display: none;
}
.address-delet-btn {
  display: none;
  margin-left: 8px;
}
.address-card:hover .address-edit-btn,
.address-card:hover .address-delet-btn {
  display: block;
}
</style>